package com.activiti.basic.config.swagger;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author dada
 * @version 1.0
 * @date 2020/4/4
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.host}")
    private String host;

    @Value("${swagger.enable}")
    private String swaggerEnable;

    @Value("${swagger.title}")
    private String title;

    @Value("${swagger.contact.name}")
    private String contactName;


    @Value("${swagger.contact.email}")
    private String contactEmail;

    private static final String AUTHORIZATION_STR = "Authorization";

    @Bean
    public Docket controllerApi() {
        boolean enableSwagger = StringUtils.isNotEmpty(swaggerEnable) && "true".equalsIgnoreCase(swaggerEnable);
        String protocol = "http";
        if (host.startsWith("https")) {
            protocol = "https";
        }
        host = host.replaceFirst("http[s]?://", "");

        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enableSwagger)
                .apiInfo(new ApiInfoBuilder()
                        .title(title)
                        .contact(new Contact(contactName, "", contactEmail))
                        .version("Version:1.0")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build().protocols(Collections.singleton(protocol)).host(host).securitySchemes(securitySchemes()).securityContexts(securityContexts());

    }


    private List<ApiKey> securitySchemes() {
        return Arrays.asList((new ApiKey(AUTHORIZATION_STR, AUTHORIZATION_STR, "header")));
    }

    private List<SecurityContext> securityContexts() {
        return Arrays.asList(SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.regex("/*.*")).build());
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Arrays.asList(new SecurityReference(AUTHORIZATION_STR, authorizationScopes));
    }
}
